package com.maxlen.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxlen.admin.entity.Permission;
import com.maxlen.admin.entity.dto.PageParamDto;
import com.maxlen.admin.entity.dto.PermissionDto;
import com.maxlen.admin.service.IPermissionService;
import com.maxlen.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色控制器
 */
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 前端根据 token 获取相应路由信息
     * @param request
     * @return
     */
    @GetMapping("/router")
    public R router(HttpServletRequest request){
        List<String> pathList = permissionService.findPathByRequest(request);
        pathList.add("/");
        return R.ok().put("data", pathList);
    }

    /**
     * 分页排序搜索查询所有列表
     * @param pageParamDto
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody PageParamDto pageParamDto){
        Page<Permission> permissionPage = permissionService.findAllByPage(pageParamDto);
        return R.ok().put("data", permissionPage);
    }

    /**
     * 添加权限
     * @param permissionDto
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody PermissionDto permissionDto){
        boolean save = permissionService.add(permissionDto);
        if(save){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        if(permissionService.deleteById(id)){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     * @param multipleId
     * @return
     */
    @PostMapping("/deleteMultiple")
    public R delete(@RequestBody List<Long> multipleId){
        if(permissionService.deleteByIdList(multipleId)){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 更新权限
     * @param permissionDto
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody PermissionDto permissionDto){
        if(permissionService.updateById(permissionDto)){
            return R.ok();
        }
        return R.error();
    }

}
